package top.lyc829.zzl_android.ui.message.system;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Canvas;
import android.os.Bundle;

import com.chad.library.adapter.base.listener.OnItemSwipeListener;

import top.lyc829.zzl_android.R;
import top.lyc829.zzl_android.databinding.ActivityNoticeSystemBinding;
import top.lyc829.zzl_android.logic.bean.NoticeSystemBean;
import top.lyc829.zzl_android.logic.database.dao.NoticeSystemDao;
import top.lyc829.zzl_android.logic.database.MyDataBase;
import top.lyc829.zzl_android.logic.util.Utils;

public class NoticeSystemActivity extends AppCompatActivity {

    ActivityNoticeSystemBinding binding;
    NoticeSystemViewModel mViewModel;
    NoticeSystemAdapter adapter;

    MyDataBase myDataBase;
    NoticeSystemDao noticeSystemDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityNoticeSystemBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        mViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication()).create(NoticeSystemViewModel.class);

        binding.mTitleBarNoticeSystem.setLeftClickListener(v -> finish());

        initRecycler();

        observer();

        mViewModel.loadNoticeSystems(Utils.getUserId());

        clickListener();
    }

    private void clickListener() {

    }

    private void observer() {

        mViewModel.getNoticeSystemsLiveData().observe(this, noticeSystemBeans -> {
            adapter.setList(noticeSystemBeans);
        });
    }

    private void initRecycler() {

        // 侧滑监听
        OnItemSwipeListener onItemSwipeListener = new OnItemSwipeListener() {
            NoticeSystemBean systemBean = null;

            @Override
            public void onItemSwipeStart(RecyclerView.ViewHolder viewHolder, int pos) {
                systemBean = adapter.getItem(pos);
            }

            @Override
            public void clearView(RecyclerView.ViewHolder viewHolder, int pos) {
            }

            @Override
            public void onItemSwiped(RecyclerView.ViewHolder viewHolder, int pos) {
                Utils.Sout("View Swiped: " + pos);
                mViewModel.deleteSystemData(systemBean);
            }

            @Override
            public void onItemSwipeMoving(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
                canvas.drawColor(ContextCompat.getColor(NoticeSystemActivity.this, R.color.zzl_red));
            }
        };

        adapter = new NoticeSystemAdapter();
        binding.noticeSystemRecycler.setLayoutManager(new LinearLayoutManager(this));
        adapter.getDraggableModule().setSwipeEnabled(true);
        adapter.getDraggableModule().setOnItemSwipeListener(onItemSwipeListener);
        adapter.getDraggableModule().getItemTouchHelperCallback().setSwipeMoveFlags(ItemTouchHelper.START | ItemTouchHelper.END);
        binding.noticeSystemRecycler.setAdapter(adapter);
        adapter.setAnimationEnable(true);   // recyclerview加载动画
    }


}